package mi.payment.ui.other;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import lainey.ui.utils.LogUtil;
import lainey.ui.widget.TitleView;
import mi.payment.R;
import mi.payment.bean.settings.MenuItem;
import mi.payment.bean.settings.MenuItemTag;
import mi.payment.constants.Configs;
import mi.payment.ui.base.ParentActivity;
import mi.payment.ui.view.sdlv.Menu;
import mi.payment.ui.view.sdlv.SlideAndDragListView;
import mi.payment.utils.SharedPreferencesUtil;

/**
 * 侧边栏排序设置
 *
 * @author by zdy on 2017/12/27.
 */
public class OrderSettingActivity extends ParentActivity implements SlideAndDragListView.OnDragDropListener {

    public static final String KEY = "sidebar_order_setting";

    private ArrayList<MenuItem> mDataList;
    private SlideAndDragListView mListView;

    private MenuItem mCurrentItem;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other_activity_sidebar_sort_setting);
        initView();
    }

    private void initView() {
        TitleView titleView = findViewById(R.id.title_view);
        titleView.setCenterText(R.string.other_sidebar_sort);
        titleView.setLeftImageOnClickListener(
                v -> onBackPressed()
        );

        // 只是为了让SlideAndDragListView源码不报错，没任何作用,SlideAndDragListView源码中已屏蔽左右滑动
        mi.payment.ui.view.sdlv.MenuItem item = new mi.payment.ui.view.sdlv.MenuItem.Builder().setWidth(200)
                .setText("One")
                .setTextColor(Color.GRAY)
                .setTextSize(14)
                .build();
        Menu menu = new Menu(true);
        menu.addItem(item);

        mDataList = SharedPreferencesUtil.readObj(KEY);
        if (mDataList == null) {
            mDataList = new ArrayList<>();
            LogUtil.e(Configs.TAG, "从SharedPreferences中拿取数据出错");
        }

        mListView = findViewById(R.id.list_view);
        mListView.setMenu(menu);
        mListView.setAdapter(mAdapter);
        mListView.setOnDragDropListener(this);

    }


    private BaseAdapter mAdapter = new BaseAdapter() {

        @Override
        public int getCount() {
            return mDataList.size();
        }

        @Override
        public Object getItem(int position) {
            return mDataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.other_item_sidebar_sort_lv, null);
                holder.ivIcon = convertView.findViewById(R.id.iv_icon);
                holder.tvName = convertView.findViewById(R.id.tv_name);
                holder.ivMenu = convertView.findViewById(R.id.iv_menu);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final MenuItem item = mDataList.get(position);

            int iconId = getIconId(item.tag);
            holder.ivIcon.setImageResource(iconId);

            if (item.strResId != 0) {
                holder.tvName.setText(item.strResId);
            }

            holder.ivMenu.setTag(position + "");
            holder.ivMenu.setOnTouchListener(mOnTouchListener);
            return convertView;
        }


        class ViewHolder {
            ImageView ivIcon;
            TextView tvName;
            ImageView ivMenu;
        }

        private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Object o = v.getTag();
                if (o != null && o instanceof Integer) {
                    Integer var = (Integer) o;
                    int intValue = var.intValue();
                    mListView.startDrag(intValue);
                }
                return false;
            }

        };

    };

    @Override
    public void onDragViewStart(int beginPosition) {
        mCurrentItem = mDataList.get(beginPosition);
    }

    @Override
    public void onDragDropViewMoved(int fromPosition, int toPosition) {
        MenuItem item = mDataList.remove(fromPosition);
        mDataList.add(toPosition, item);
    }

    @Override
    public void onDragViewDown(int finalPosition) {
        mDataList.set(finalPosition, mCurrentItem);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 发现在onDestroy的时候再保存，如果操作速度很快，数据并没有保存进去就会被再次读出，导致数据未更新
        SharedPreferencesUtil.saveObj(mDataList, KEY);
    }

    private int getIconId(int tag) {
        int resId;
        switch (tag) {
            case MenuItemTag.Common.LOGIN:
                resId = R.drawable.menu_login_red;
                break;
            case MenuItemTag.Common.SIGN:
                resId = R.drawable.menu_sign_press;
                break;
            case MenuItemTag.Common.SALE:
                resId = R.drawable.menu_sale_red;
                break;
            case MenuItemTag.Common.PRE_AUTH:
                resId = R.drawable.menu_pre_auth_red;
                break;
            case MenuItemTag.Common.QUERY_QR_CODE:
                resId = R.drawable.menu_query_qr_code_red;
                break;
            case MenuItemTag.Common.VOID:
                resId = R.drawable.menu_revoke_red;
                break;
            case MenuItemTag.Common.QUERY_TRANSACTION:
                resId = R.drawable.menu_query_online_red;
                break;
            case MenuItemTag.Common.REFUND:
                resId = R.drawable.menu_refund_red;
                break;
            case MenuItemTag.Common.PRINT:
                resId = R.drawable.menu_print_red;
                break;
            case MenuItemTag.Common.BALANCE:
                resId = R.drawable.menu_query_balance_red;
                break;
            case MenuItemTag.Common.SETTLEMENT:
                resId = R.drawable.menu_settlement_red;
                break;
            case MenuItemTag.Common.MANAGE:
                resId = R.drawable.menu_manage_red;
                break;
            case MenuItemTag.Common.OTHER:
                resId = R.drawable.menu_other_red;
                break;
            default:
                resId = R.drawable.menu_other_red;
                break;
        }
        return resId;
    }


}
